package com.newenergy.wisehome.ui.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.newenergy.wisehome.R;
import com.newenergy.wisehome.common.util.ImageUtil;
import com.newenergy.wisehome.data.bean.DeviceCategory;

import java.util.List;

/**
 * 设备适配器
 * Created by new on 2017/11/30.
 */

public class DeviceAdapter extends RecyclerArrayAdapter<DeviceCategory> {

    private ItemOperCallBack mItemOperCallBack;
    private int mDataPosition;
    private Context mContext;


    public void refreshData(List<DeviceCategory> data) {
        clear();
        if (data != null) addAll(data);
        notifyDataSetChanged();
    }

    public DeviceAdapter(Context context) {
        super(context);
        mContext=context.getApplicationContext();
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int
            viewType) {
        return new ViewHolder(parent);
    }

    private class ViewHolder extends BaseViewHolder<DeviceCategory> {

        private TextView tvName;
        private ImageView ivDeleteDevice;
        private ImageView ivAddDevice;
        private ImageView ivIcon;


        public ViewHolder(ViewGroup parent) {
            super(parent, R.layout.list_device);
            tvName = $(R.id.tv_name);
            ivDeleteDevice = $(R.id.iv_delete_device);
            ivAddDevice = $(R.id.iv_add_device);
            ivIcon=$(R.id.iv_icon);


            ivAddDevice.setOnClickListener(v -> {
                if (mItemOperCallBack!=null){
                    mItemOperCallBack.notifyAdd(getDataPosition());
                }

            });

            ivDeleteDevice.setOnClickListener(v -> {
                if (mItemOperCallBack!=null){
                    mItemOperCallBack.notifyDelete(getDataPosition());
                }

            });

            ivDeleteDevice.setOnTouchListener((v, event) -> {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    mDataPosition = getDataPosition();
                    downTime = System.currentTimeMillis();
                    onBtnTouch = true;
                    Thread t = new Thread(mDelRunnable);
                    t.start();
                } else if (event.getAction() == MotionEvent
                        .ACTION_UP) {
                    onBtnTouch = false;
                    if (thisTime - downTime < 500) {
//                        sendEmptyMessage(1000);
                    }
                } else if (event.getAction() == MotionEvent
                        .ACTION_CANCEL) {
                    onBtnTouch = false;
                }
                return false;
            });

        }

        @Override
        public void setData(DeviceCategory data) {
            super.setData(data);
            tvName.setText(data.getName());
            int imageId = ImageUtil.getImageId(data.getIcName());
//            ivIcon.setImageResource(imageId);
            Glide.with(mContext).load(imageId).diskCacheStrategy(DiskCacheStrategy.NONE).into(ivIcon);

        }
    }


    public void setItemOperCallBack(ItemOperCallBack itemOperCallBack) {

        mItemOperCallBack = itemOperCallBack;
    }

    public interface ItemOperCallBack {
        void notifyAdd(int position);

        void notifyDelete(int position);

        void notifyForceDelete(int position);
    }

    long downTime = 0;//Button被按下时的时间
    long thisTime = 0;//while每次循环时的时间
    boolean onBtnTouch = false;//Button是否被按下



    public Runnable mDelRunnable = () -> {
        while (onBtnTouch) {
            thisTime = System.currentTimeMillis();
            if (thisTime - downTime >= 5000) {
                sendEmptyMessage(1001);
                onBtnTouch = false;
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };

    private void sendEmptyMessage(int what) {
        handler.sendEmptyMessage(what);
    }


    public Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1000:
                    if (mItemOperCallBack!=null){
                        mItemOperCallBack.notifyDelete(mDataPosition);
                        removeCallbacks(mDelRunnable);
                    }

                    break;
                case 1001:
                    if (mItemOperCallBack!=null){
                        mItemOperCallBack.notifyForceDelete(mDataPosition);
                        removeCallbacks(mDelRunnable);
                    }

                    break;
            }

        }
    };

}
